*WHAT'S ACROSS THE BORDER?
*JHA, NEUMARK, AND RODRIGUEZ-LOPEZ

include directories.do

***********************************************
*Rolling regressions
***********************************************


foreach u in "county" {
* Load stacked pair data at the county or czone-state level
use "`datadir'`s'cbp_stacked_`u'pair_sample.dta", clear

***********************************************
*GENERATE VARIABLES (SAME FOR COUNTY OR CZONE)
***********************************************


gen lmw=log(mw)

*Generate totempm, which removes emp of the industry of interest
foreach b in "emp" {
gen tot`b'm=tot`b'-`b'
}

foreach b in "emp" "wageap" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
gen ltot`b'm=log(tot`b'm)
}

gen lworkagepop=log(workagepop)


********************************************
**** DEFINE LOCAL VARIABLES
********************************************

local period year

*Dependent variable and regressors

local mwvar lmw
local popvar lworkagepop
local wagevar lwageap
local empvar lemp
local totempvar ltotempm
local totwagevar ltotwageapm


*Merge HHI monopsonypower data


*Demeaned data
merge m:1 county using "`hhidir'`s'hhi92_county_rest_dem.dta"
drop _merge
drop if countypair==.



*MP means "monopsony power"
local MPvar hhi92
gen lmw_MP=lmw*`MPvar'
local mwMPvar lmw_MP

foreach j in "1" "2" "3" "4" "5" {
gen lmw_hhi92_`j'_d=lmw*hhi92_`j'_d
local MPvar_`j'_d hhi92_`j'_d
local mwMPvar_`j'_d lmw_hhi92_`j'_d
}



********* MERGE PAIRS ********************
drop state
merge m:1 countypair_id county using "`countydir'`s'countypairtypes.dta"

drop _merge


gen state=int(county/1000)
bys countypair_id: egen state1=min(state)
bys countypair_id: egen state2=max(state)

**************************************************
****STACKED REGRESSIONS WITH REGHDFE
**************************************************

sort  countypair `period'

egen bordersegment = group(state1 state2)
egen pairperiod=group(`u'pair `period')


*Specify clustering
local options "vce(cluster state bordersegment) absorb(`u' pairperiod)"


*Specifications

foreach j in "1" "2" "3" "4" "5" {

foreach h in "emp" {

local controls "`tot`h'var' `popvar'"


eststo m_`h'_`j': reghdfe ``h'var' `mwvar' `mwMPvar_`j'_d' `controls' if (type`j'==1) & `period'>1991, `options'


}

}


}

/* Print results */

tempfile specs

#delimit ;
esttab m*  using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Pair approach estimation -- HHI")
	order(`mwvar' `mwMPvar_1_d' `mwMPvar_2_d' `mwMPvar_3_d' `mwMPvar_4_d' `mwMPvar_5_d')
	nonotes
	label
	stats(N, labels("N") fmt(0))	
	replace;
#delimit cr

* Switch the output to Excel format
insheet using "`specs'.csv", comma clear
export excel "`outdir'`s'TablesJNR.xlsx", sheetreplace sheet(TableC1)

clear all


use "`hhidir'`s'hhi92_county_rest_dem.dta"
merge 1:1 county using "`countydir'`s'countytypes.dta"
foreach j in "1" "2" "3" "4" "5" {
sum hhi92 if type`j'==1
}

clear all






